package com.guchenbo.example.thread;

import cn.hutool.core.date.DateUtil;

import java.util.concurrent.*;

public class QueueDemo {
    public static void main(String[] args) throws InterruptedException {
        //        LinkedBlockingQueue<Integer> queue = new LinkedBlockingQueue<>(10);
        ////        queue.take();
        //
        //        queue.poll();
        //
        //        queue.poll(3, TimeUnit.SECONDS);
        //        queue.poll(0, TimeUnit.SECONDS);
        //
        //        System.out.println("end");
        //        arrayBlockingQueue();
        //                synchronousQueue();
        //                priorityBlockingQueue();
        System.out.println(Long.MAX_VALUE >> 1);
        delayQueue();
    }

    private static void delayQueue() {
        ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
//        executor.schedule(() -> System.out.println("me"), 3, TimeUnit.SECONDS);
//        executor.schedule(() -> System.out.println("me 2"), 5, TimeUnit.SECONDS);

        System.out.println(DateUtil.now());
        executor.scheduleAtFixedRate(() -> {
            System.out.println(DateUtil.now());
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, 1, 3, TimeUnit.SECONDS);

    }

    static class DelayObj implements Delayed {

        @Override
        public long getDelay(TimeUnit unit) {
            return 1000;
        }

        @Override
        public int compareTo(Delayed o) {
            return this.compareTo(o);
        }
    }

    private static void arrayBlockingQueue() throws InterruptedException {
        ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<>(3);
        queue.put("a");
        queue.put("b");
        queue.put("c");
        queue.put("d");
    }

    private static void priorityBlockingQueue() throws InterruptedException {
        PriorityBlockingQueue<String> queue = new PriorityBlockingQueue<>();
        queue.put("a");
        queue.put("b");
        queue.put("c");

        System.out.println(queue.take());
    }

    private static void synchronousQueue() throws InterruptedException {
        SynchronousQueue<String> queue = new SynchronousQueue<>();
        //        System.out.println("offer");
        //        queue.take();
        //        System.out.println("take");
        //        queue.put("a");
        //        System.out.println("put a");
        //        queue.put("b");
        //        System.out.println("put b");
        new Thread(() -> {
            try {
                while (true) {
                    System.out.println(queue.take());
                    System.out.println("take");
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }).start();
        while (true) {
            if (queue.offer("a")) {
                System.out.println("br");
                break;
            }
        }
    }
}
